package com.dog.basics.netty.init;


import com.dog.basics.netty.agreement.DogProtobufDecoder;
import com.dog.basics.netty.agreement.DogProtobufEncoder;
import com.dog.basics.netty.consts.Const;
import com.dog.basics.netty.handel.DogNewServerHandle;
import com.dog.basics.netty.protobuf.dto.DogNewsContentDto;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.protobuf.ProtobufDecoder;
import io.netty.handler.codec.protobuf.ProtobufEncoder;
import io.netty.handler.timeout.IdleStateHandler;
import java.util.concurrent.TimeUnit;

/**
 * @program: d-basic-compositionzzz
 * @create: 2019-08-27 16:20
 **/
public class SocketChannelInitializer extends ChannelInitializer<SocketChannel> {

	@Override
	protected void initChannel(SocketChannel ch) {
		ch.pipeline()
				// 心跳检测 客户端没有向服务器发送心跳则关闭连接
				.addLast(new IdleStateHandler(Const.maxIdleTime, Const.maxIdleTime, Const.maxIdleTime, TimeUnit.SECONDS))
				// 解码
				.addLast(new DogProtobufDecoder(Const.acceptMaxMsgLength, Const.headFieldLength, Const.msgFieldLength, Const.lengthAdjustment, 0, false))
				.addLast(new ProtobufDecoder(DogNewsContentDto.Message.getDefaultInstance()))
				// 编码
				.addLast(new DogProtobufEncoder())
				.addLast(new ProtobufEncoder())
				.addLast(new DogNewServerHandle());
	}
}
